package com.danger.Activity.Account;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.danger.Activity.BackHandledFragment;
import com.danger.Activity.R;
import com.danger.Model.bean.UserInfoBean;
import com.danger.Presenter.UserInfoPresenter;
import com.danger.Presenter.UserInfoPresenterImpl;
import com.danger.Util.FileCacheService;
import com.danger.Util.ImageUtil.ImageLoaderUtil;
import com.danger.View.CommonView;
import com.nostra13.universalimageloader.core.assist.ImageSize;

import cn.bmob.v3.BmobUser;

/**
 * Created by danger on 2016/4/26.
 */
public class ModifyPasswordFragment extends BackHandledFragment implements CommonView, View.OnClickListener {

    private boolean hadIntercept;
    private View view;
    private ProgressDialog dialog;
    private Toolbar toolbar;
    private EditText new_password_editText;
    private EditText new_confirm_password_editText;
    private Button modify_username_button;
    private UserInfoActivity activity;
    private UserInfoPresenter presenter;
    private UserInfoBean userInfo;
    private FileCacheService service;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_modify_password, container, false);

        initView();
        initEvent();

        return view;
    }


    private void initView() {
        activity = (UserInfoActivity) getActivity();
        service = new FileCacheService(activity);
        toolbar = activity.getToolbar();
        toolbar.setTitle("修改密码");
        activity.setSupportActionBar(toolbar);
        activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        dialog = new ProgressDialog(activity);
        dialog.setMessage("正在提交修改i...");
        dialog.setCanceledOnTouchOutside(false);
        new_password_editText = (EditText) view.findViewById(R.id.new_password_editText);
        new_confirm_password_editText = (EditText) view.findViewById(R.id.new_confirm_password_editText);
        modify_username_button = (Button) view.findViewById(R.id.modify_username_button);
        presenter = new UserInfoPresenterImpl(activity, this);
        userInfo = BmobUser.getCurrentUser(activity, UserInfoBean.class);
        if (service.getSharePreference("setting").get("theme").equals("2")) {
            activity.getUserNameTexTView().setText(userInfo.getUsername().toString());
            activity.getUserPhoneTextView().setText(userInfo.getMobilePhoneNumber().toString());
            if (userInfo.getPhoto() != null) {
                new ImageLoaderUtil(new ImageSize(90, 90), activity.getMyPhotoView(), userInfo.getPhoto(), activity).load();
            }
        }
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });
        modify_username_button.setOnClickListener(this);
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }

    @Override
    public void showDialog() {
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.hide();
        dialog.dismiss();
    }

    @Override
    public void showSuccess(String message, String object) {
        Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showError(String error) {
        Toast.makeText(activity, error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void back() {
        getFragmentManager().popBackStack();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.modify_username_button:
                presenter.updatePassword(new_password_editText.getText().toString(), new_confirm_password_editText.getText().toString(), userInfo.getObjectId());
                break;
            default:
                break;
        }
    }
}
